﻿using MediatR;
using PLC.Domain.Models;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.Events.Handlers;

public class SaveDeviceStatusHandler:INotificationHandler<SaveDeviceStatusModel>
{
    private readonly IDeviceStatusRepository _deviceStatusRepository;

    public SaveDeviceStatusHandler(IDeviceStatusRepository deviceStatusRepository)
    {
        _deviceStatusRepository = deviceStatusRepository?? throw new ArgumentNullException(nameof(deviceStatusRepository));
    }
    /// <summary>
    /// 保存设备状态数据
    /// </summary>
    /// <param name="notification"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public async Task Handle(SaveDeviceStatusModel notification, CancellationToken cancellationToken)
    {
        if (notification.DeviceStatusList.Count==0)
        {
            return;
        }
        // 先保存一份副本到redis  然后每次值存储改变的数据

        await _deviceStatusRepository.CreateAsync(notification.DeviceStatusList);
    }
}